/**
 * Created by apple on 16/9/29.
 */

window.onload = function () {
    $('#container').children('.interest-list').html('');
    data_motocycle = {
        page: 1,
        rows: 20
    };
    //所有关注的类别
    interest_list_all();

    //文章列表
    article_list_interest();

    //精华  按钮
    $("#onlyEssence").change(function () {
        $('#container').children('.interest-list').html('');
        $('#load-more-topic').show();
        var _text = $('.interest-manage .cur').html();
        if ($(this).is(':checked') == true) {
            //表明已经选中为"精华帖"
            if (_text == "全部发现") {
                //全部发现
                data_motocycle = {
                    page: 1,
                    rows: 20,
                    is_nice: 1
                }
            } else {
                //我的发现
                data_motocycle = {
                    page: 1,
                    rows: 20,
                    is_nice: 1,
                    is_type: 1,
                    user_id: user_id
                }
            }
        } else {
            //不是"精华帖"
            if (_text == "全部发现") {
                //全部发现
                data_motocycle = {
                    page: 1,
                    rows: 20
                }
            } else {
                //我的发现
                data_motocycle = {
                    page: 1,
                    rows: 20,
                    is_type: 1,
                    user_id: user_id
                };
            }
        }
        //文章列表
        article_list_interest();
    });
};

//"全部发现""我的发现"按钮的点击
$(document).on('click', '.interest-manage a', function () {
    $('#container').children('.interest-list').html('');
    $('#load-more-topic').show();
    $('.interest-manage a').removeClass('cur');
    $(this).addClass("cur");
    if ($(this).html() == "全部发现") {
        //全部发现
        if ($('#onlyEssence').is(':checked') == true) {
            //表明已经选中为"精华帖"
            data_motocycle = {
                page: 1,
                rows: 20,
                is_nice: 1
            };
        } else {
            //不是"精华帖"
            data_motocycle = {
                page: 1,
                rows: 20
            };
        }
        interest_list_all();
    } else {
        //我的发现
        if (user_id) {
            if ($('#onlyEssence').is(':checked') == true) {
                //表明已经选中为"精华帖"
                data_motocycle = {
                    page: 1,
                    rows: 20,
                    is_nice: 1,
                    is_type: 1,
                    user_id: user_id
                };
            } else {
                //不是"精华帖"
                data_motocycle = {
                    page: 1,
                    rows: 20,
                    is_type: 1,
                    user_id: user_id
                };
            }
            interest_list_mine();
        } else {
            window.location.href = "loading.html"
        }

    }
    article_list_interest();
});

//点击"按发现的内容查看帖子"按钮
$(document).on('click', '.view-btn', function () {
    if ($('.interest-item').hasClass('show')) {
        $('.interest-item').hide();
        $('.interest-item').removeClass('show');
    } else {
        $('.interest-item').show();
        $('.interest-item').addClass('show');
    }
});

//点击具体的发现内容,如:哈雷摩托,越野车,飞机车等
$(document).on('click', '.big-class-list li', function () {
    $('.interest-item').hide();
    $('.interest-item').removeClass('show');
});

//加载更多
$(document).on('click', '#load-more-topic', function () {
    page = page + 1;
    if (!data_motocycle.is_type) {
        //全部发现
        if ($('#onlyEssence').is(':checked') == true) {
            //表明已经选中为"精华帖"
            data_motocycle = {
                page: page,
                rows: 20,
                is_nice: 1
            };
        } else {
            //不是"精华帖"
            data_motocycle = {
                page: page,
                rows: 20
            };
        }
    } else {
        //我的发现
        if ($('#onlyEssence').is(':checked') == true) {
            //表明已经选中为"精华帖"
            data_motocycle = {
                page: page,
                rows: 20,
                is_nice: 1,
                is_type: 1,
                user_id: user_id
            };
        } else {
            //不是"精华帖"
            data_motocycle = {
                page: page,
                rows: 20,
                is_type: 1,
                user_id: user_id
            };
        }
    }
    article_list_interest();
});

//发现的类别列表(所有)
function interest_list_all() {
    $('.big-class-list').html('');
    $.get(url_link + 'mototypes', function (data) {
        try {
            var obj = JSON.parse(data);
            var interest_list = "";
            if (obj.code == 0) {
                $.each(obj.data, function (i, item) {
                    interest_list += "<li><a href='discover_theme.html?mototype_id=" + item.mototype_id + "' target='_blank'>" + item.name + "</a></li>"
                });
                $('.big-class-list').html(interest_list)
            }
        } catch (e) {
        }
    })
}

//发现的类别列表(我所关注的)
function interest_list_mine() {
    $('.big-class-list').html('');
    $.get(url_link + 'mototypelikes', {user_id: user_id}, function (data) {
        try {
            var obj = JSON.parse(data);
            var interest_list = "";
            if (obj.code == 0) {
                $.each(obj.data, function (i, item) {
                    interest_list += "<li><a href='discover_theme.html?mototype_id=" + item.mototype_id + "' target='_blank'>" + item.name + "</a></li>"
                });
                $('.big-class-list').html(interest_list)
            }
        } catch (e) {

        }
    })
}

//发现的内容页展示
function article_list_interest() {
    $.ajax({
        url: url_link + 'articles',
        data: data_motocycle,
        async: false,
        success: function (data) {
            var obj = JSON.parse(data);
            var interest_list = "";
            if (obj.code == 0) {
                if (obj.data.rows.length != 0) {
                    if (obj.data.rows.length < data_motocycle.rows) {
                        $('#load-more-topic').hide();
                    }
                    $.each(obj.data.rows, function (i, item) {
                        interest_list += "<li data-article=" + item.article_id + ">" +
                            "<div class='img-box'>" +
                            "<div class='interest-label'>" +
                            "<div class='label'>" +
                            "<a href='discover_theme.html?mototype_id=" + item.mototype_id + "' target='_blank'>" + item.mototype_name + "</a>" +
                            "</div>" +
                            "</div>" +
                            "<a href='publish/show.html?article_id=" + item.article_id + "' target='_blank'>";
                        if (item.cover) {
                            interest_list += "<img src='" + item.cover + "' style='width: 252px;height: 168px'>";
                        } else {
                            var _content = item.content.split("$$##MotorTravel##$$");
                            var _count = 0;
                            for (var _a = 0; _a < _content.length; _a++) {
                                var m = _content[_a].substring(0, 13);
                                if (m == "$$##Image##$$") {
                                    //有图
                                    _count = 1;
                                    var _content_image_description = _content[_a].split("$$##ImageDescription##$$");
                                    var _content_image_url = _content_image_description[0].split("$$##ImageInfo##$$");
                                    interest_list += "<img src=" + _content_image_url[0].substring(13, _content_image_url[0].length) + " style='width: 256px;height: 168px;'>"
                                    break;
                                }
                            }
                            //纯文字,没有图的情况下,使用备用图片
                            if (_count == 0) {
                                interest_list += "<img src='../image/base/none_img.png' style='width: 252px;height: 168px'>"
                            }
                        }


                        interest_list += "</a>" +
                            "<div class='about-count'>" +
                            "<span class='click'><i></i>" + item.see_count + "</span>" +
                            "<span class='comment'><i></i>" + item.comment_count + "</span>" +
                            "<span class='praise'><i></i>" + item.gift_count + "</span>" +
                            "</div>" +
                            "</div>" +
                            "<h4>" +
                            "<a href='publish/show.html?article_id=" + item.article_id + "' target='_blank'>"
                        if (item.is_nice == 1) {
                            interest_list += "<i class='essence'></i>"
                        }

                        interest_list += item.title + "</a></h4><div class='user-info'>" +
                            "<a href='lookat/article.html?user_id=" + item.user_id + "' class='user' target='_blank'><i>";
                        if (item.user.avatar) {
                            interest_list += "<img src=" + item.user.avatar + " style='width: 25px;height: 25px;'>"
                        } else {
                            interest_list += "<img src='../image/base/none.png' style='width: 25px;height: 25px;'>"
                        }

                        interest_list += "</i><em>" + item.user.nickname + "</em></a>";
                        //判断是否是今日更新
                        var updated_time = item.updatedDate.substring(0, 10);
                        if (mytime == updated_time) {
                            item.updatedDate = "今天   " + item.updatedDate.substring(11, item.updatedDate.length)
                        }
                        interest_list += "<span class='pub-time'>" + item.updatedDate + "</span></div></li>";
                    });
                    if ($('#container').children('.interest-list').children('li').length == 0) {
                        $('#container').children('.interest-list').html(interest_list)
                    } else {
                        $('#container').children('.interest-list').append(interest_list)
                    }
                } else {
                    if ($('.interest-list li').length == 0) {
                        //一开始这个页面就没有任何内容
                        $('.interest-list').html("<h2 class='none_info'>暂时没有相关文章展示</h2>")
                    } else {
                        //预防上一页obj.data.length = data_motocycle.rows的情况出现
                        alert('没有内容啦!去看看别的吧!');
                    }
                    $('#load-more-topic').hide();
                }
                page = data_motocycle.page;
            } else {

            }
        }
    });
}